package com.shareroad.dubboweb.frame.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shareroad.dubboweb.frame.base.Constants;
import com.shareroad.dubboweb.frame.dao.data.DubboWebData;
import com.shareroad.dubboweb.frame.handler.ApiHandler;
import com.shareroad.dubboweb.frame.model.entity.ReturnObject;
import com.shareroad.dubboweb.frame.model.enums.ErrorCode;

@Controller
public class ApiController {
	
	@Autowired
	private ApiHandler apiHandler;
    
    @ResponseBody
    @RequestMapping("/api")
	public String api(
			@RequestParam(value="_i",required=false) String interfaceName,
			@RequestParam(value="_v",required=false) String version,
			@RequestParam(value="_m",required=false) String method,
			@RequestParam(value="_p",required=false,defaultValue="{}") String params){
    	ReturnObject ro = new ReturnObject();
    	try{
	    	if(StringUtils.isEmpty(interfaceName)){
	    		ro.setStatus(Constants.HTTP_500);
	    		ro.setErrorCode(ErrorCode.api_param_interface_miss.getCode());
	    		ro.setErrorDesc(ErrorCode.api_param_interface_miss.getDesc());
	    		return JSON.toJSONString(ro);
	    	}
	    	
	    	if(StringUtils.isEmpty(method)){
	    		ro.setStatus(Constants.HTTP_500);
	    		ro.setErrorCode(ErrorCode.api_param_method_miss.getCode());
	    		ro.setErrorDesc(ErrorCode.api_param_method_miss.getDesc());
	    		return JSON.toJSONString(ro);
	    	}
	    	
	    	JSONObject paramObject = null;
	    	try{
	    		paramObject = JSONObject.parseObject(params);
	    	}catch(Exception e){
	    		ro.setStatus(Constants.HTTP_500);
	    		ro.setErrorCode(ErrorCode.api_param_params_illegal.getCode());
	    		ro.setErrorDesc(ErrorCode.api_param_params_illegal.getDesc());
	    		return JSON.toJSONString(ro);
	    	}
	    	try{
	    		JSONObject result = apiHandler.invokeDubboService(interfaceName,version, method, paramObject);
	    		ro.setStatus(Constants.HTTP_200);
	    		ro.setReturnContent(JSONObject.toJSONString(result));
	    		return JSON.toJSONString(ro);
	    	}catch(Exception e){
	    		ro.setStatus(Constants.HTTP_500);
	    		ro.setErrorCode(ErrorCode.api_dubbo_service_error.getCode());
	    		ro.setErrorDesc(ErrorCode.api_dubbo_service_error.getDesc());
	    		return JSON.toJSONString(ro);
	    	}
    	}catch(Exception e){
    		ro.setStatus(Constants.HTTP_500);
    		ro.setErrorCode(ErrorCode.api_system_error.getCode());
    		ro.setErrorDesc(ErrorCode.api_system_error.getDesc());
    		return JSON.toJSONString(ro);
    	}
	}
    
    @ResponseBody
    @RequestMapping("/error")
    public String error(){
    	ReturnObject ro = new ReturnObject();
    	ro.setStatus(Constants.HTTP_404);
		ro.setErrorCode(ErrorCode.api_url_error.getCode());
		ro.setErrorDesc(ErrorCode.api_url_error.getDesc());
		return JSON.toJSONString(ro);
    }
    
    @ResponseBody
    @RequestMapping("/clients")
    public String clients(){
    	ReturnObject ro = new ReturnObject();
    	ro.setStatus(Constants.HTTP_200);
		ro.setReturnContent(JSON.toJSONString(DubboWebData.getAllData()));
		return JSON.toJSONString(ro);
    }

    
}
